package twodimension;

public class DiagonalLineDemo {

    public static void main(String[] args) {

        int[][] arr = {{1, 2, 3, 4}, {3, 4, 5, 6}, {5, 6, 7, 8}, {7, 8, 9, 0}};
        int sum = 0;

        for (int i = 0; i < arr.length; i++) {

            sum += arr[i][i] + arr[i][arr.length - 1 - i];

        }

        if (arr.length % 2 != 0) {  //奇数行。中间的元素多加了一次。
            sum -= arr[arr.length / 2][arr.length / 2];
        }

        System.out.println("对角线元素之和是：" + sum);

    }
}
